package com.testapp.bluetoothtest08.File;

import android.bluetooth.BluetoothDevice;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class DeviceFile {
    File deviceDay[] = new File[14];
    File deviceTime[] = new File[14];
    File index = new File("/data/data/com.testapp.bluetoothtest08/deviceIndex.txt");//当前的天数索引
    BaseFileOperation bfo = new BaseFileOperation();
    int day;

    public DeviceFile() {
        init();
    }

    //初始化，如果没有这个文件则初始化文件
    public void init() {
        for (int i = 0; i < 14; i++) {
            deviceDay[i] = new File("/data/data/com.testapp.bluetoothtest08/deviceDay" + i + ".txt");//存放每天的设备
            if (!bfo.isExist(deviceDay[i])) {
                bfo.write(deviceDay[i], "");
            }
        }
        for (int i = 0; i < 14; i++) {
            deviceTime[i] = new File("/data/data/com.testapp.bluetoothtest08/deviceTime" + i + ".txt");//存放设备创建的时间
            if (!bfo.isExist(deviceTime[i])) {
                bfo.write(deviceTime[i], "");
            }
        }
        if (!bfo.isExist(index)) {
            bfo.write(index, "");
        }
        day = readIndex();
    }

    //写时间
    public void writeTime(int i) {
        long createdTime = System.currentTimeMillis();
        bfo.write(deviceTime[i], Long.toString(createdTime));
    }

    //读时间
    public long readTime(int i) {
        return bfo.readLong(deviceTime[i]);
    }

    //读取所有时间
    public String readAllTime() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 14; i++) {
            sb.append(readTime(i) + ";");
        }
        return sb.toString();
    }


    //写天数
    public void writeIndex() {
        bfo.write(index, Integer.toString(day));
    }

    //读天数
    public int readIndex() {
        return bfo.readInt(index);
    }


    //写入搜索设备
    public void writeDevices(List<BluetoothDevice> devices) {
//        System.out.println("into write Devices");
        //如果今天文件创建的时间没有则进行赋值
        if (readTime(day) == 0) {
            writeTime(day);
        }
        //如果今天文件存在了14天则清空
        if (System.currentTimeMillis() - readTime(day) == 60*60*24*1000*14) {
            bfo.write(deviceDay[day], "");
            writeTime(day);
        }
        //如果今天文件存在了1天则到下一天
        Long l = System.currentTimeMillis() - readTime(day);
//        System.out.println("时间相减time ----- > "+l);
        if (l >= 60*60*24*1000) {
            day++;
            if (day == 14) {
                day = 0;
            }
            writeIndex();
        }
        //写搜到的设备
        for (BluetoothDevice device : devices) {
            if (device.getName() != null) {
                System.out.println(readAllDevice());
                if (!readAllDevice().contains(device.getName())) {
                    try {
                        FileWriter fileWriter = new FileWriter(deviceDay[day], true);
                        fileWriter.write(device.getName() + ";");
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    //读取全部设备
    public String readAllDevice() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 14; i++) {
            sb.append(bfo.readString(deviceDay[i]));
        }
        return sb.toString();
    }

    public void clearAll() {
        bfo.write(index, "");
        for (int i = 0; i < 14; i++) {
            bfo.write(deviceDay[i], "");
        }
        for (int i = 0; i < 14; i++) {
            bfo.write(deviceTime[i], "");
        }
    }

}
